package com.example.springlearn.oj;

import com.example.springlearn.bean.QuestionInfo;
import org.hentai.oj.bean.OjParamName;
import org.hentai.oj.service.OjService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class OjRun {

    @Resource
    OjService ojService;


    public List<Map<Boolean, String>> judge(String filename, String code, String codeType, QuestionInfo question) throws IOException, InterruptedException {

        boolean b = ojService.saveCodeFile(filename, code, codeType);
        List<Map<Boolean, String>> ans = new ArrayList<>();
        if (!b) {
            ans.add(new HashMap<Boolean, String>() {{
                put(false, "文件保存失败");
            }});
            return ans;
        }

        Map<String, String> orders = ojService.getCompileAndRunOrder(filename, codeType);
        if (orders.containsKey(OjParamName.COMPILE)) {
            Map<Boolean, String> compile = ojService.compile(orders.get(OjParamName.COMPILE));
            if (compile.containsKey(false)) {
                ans.add(compile);
                return ans;
            }
        }

        String runOrder = orders.get(OjParamName.RUN);


        if (question.getCaseIn() == null || question.getCaseIn().equals("")) {
            Map<Boolean, String> run = ojService.run(runOrder, question.getOut1());
            ans.add(run);
            return ans;
        }

        Integer testNum = question.getTestNum();
        if (testNum > 0) {
            Map<Boolean, String> run = ojService.run(runOrder, question.getIn1(), question.getOut1());
            ans.add(run);
        }

        if (testNum > 1) {
            Map<Boolean, String> run = ojService.run(runOrder, question.getIn2(), question.getOut2());
            ans.add(run);
        }

        if (testNum > 2) {
            Map<Boolean, String> run = ojService.run(runOrder, question.getIn2(), question.getOut2());
            ans.add(run);
        }

        return ans;
    }


}
